﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.DeliveryModules
{
    public class DeliveryDetailUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<DeliveryDetailModel> GetDeliveryDetailUpdater()
        {
            NormalDataUpdater<DeliveryDetailModel> systemDataUpdater = new NormalDataUpdater<DeliveryDetailModel>();
            systemDataUpdater.DataUpdating += OrderDetailValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += OrderValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<DeliveryDetailModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]正在生效，不能删除", e.Delete.AutoId);
            }
        }

        private void StateValid(object sender,DataValidationEventArgs<DeliveryDetailModel> e)
        {
            string message;
            DeliveryFetcher deliveryFetcher = new DeliveryFetcher();
            DeliveryModel model = deliveryFetcher.Get(e.Insert.Site, e.Insert.DeliveryId, out message);
            if (e.Insert.AutoId == 0 && model != null && model.State != Definition.STATE_NORMAL)
            {
                e.Message = "已审核的表单不能新增明细行";
                e.Cancle = true;
                return;
            }
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                if (e.Insert.PropertyUpdated.Contains("ProductionId") ||
                    e.Insert.PropertyUpdated.Contains("DeliveryId") ||
                    e.Insert.PropertyUpdated.Contains("Quantity1") ||
                    e.Insert.PropertyUpdated.Contains("Quantity2") ||
                    e.Insert.PropertyUpdated.Contains("OrderId") ||
                    e.Insert.PropertyUpdated.Contains("CustomerId") ||
                    e.Insert.PropertyUpdated.Contains("OrderDetailId"))
                {
                    e.Message = "已提审/已审核的表单不能修改";
                    e.Cancle = true;
                    return;
                }
            }
            if (e.Insert.State == Definition.STATE_NORMAL)
            {
                if(e.Insert.AutoId!=0)
                {
                    ServiceOrderDetailFetcher serviceOrderDetailFetcher = new ServiceOrderDetailFetcher();
                    IList<ServiceOrderDetailModel> sodModels = serviceOrderDetailFetcher.ListByDeliveryDetail(e.Insert.Site,e.Insert.AutoId,out message, out rows);
                    if(sodModels.Count>0)
                    {
                        e.Message = "已经产生下游单据的表单无法反审核";
                        e.Cancle = true;
                        return;
                    }
                }
            }
        }
        private void OrderDetailValid(object sender, DataValidationEventArgs<DeliveryDetailModel> e)
        {
            OrderDetailFetcher orderDetailFetcher = new OrderDetailFetcher();
            string message;
            OrderDetailModel orderDetailModel = orderDetailFetcher.Get(e.Insert.Site, e.Insert.OrderDetailId, out message);
            if(orderDetailModel== null
                ||orderDetailModel.State == Definition.STATE_NORMAL
                || orderDetailModel.State == Definition.STATE_READY)
            {
                e.Cancle = true;
                e.Message = String.Format("上游单据不存在或状态不正确：{0}",message);
                return;
            }
            if(orderDetailModel.ProductionId != e.Insert.ProductionId
                ||e.Insert.OrderId != orderDetailModel.OrderId)
            {
                e.Cancle = true;
                e.Message = "上游单据的产品信息与本单据的产品信息不一致";
                return;
            }
        }
        private void OrderValid(object sender, DataValidationEventArgs<DeliveryDetailModel> e)
        {
            OrderFetcher orderFetcher = new OrderFetcher();
            string message;
            OrderModel orderModel = orderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
            if (orderModel == null
                ||orderModel.State==Definition.STATE_READY
                ||orderModel.State == Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("上游单据不存在或状态不正确：", message);
                return;
            }
            if (orderModel.CustomerId != e.Insert.CustomerId)
            {
                e.Cancle = true;
                e.Message = "上游单据的客户信息与本单据的客户信息不一致";
                return;
            }
        }
    }
}
